package com.shop.manager.controller;

//import com.shop.manager.entity.FreightTemplate;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.FreightTemplate;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.FreightTemplateService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 运营模板增删改查
 */
@RestController
@RequestMapping("/freightTemplate")
public class FreightTemplateController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private FreightTemplateService freightTemplateService;

    /**
     * @Description 添加运营模板
     * @param freightTemplate 运营模板对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) @ApiParam(value = "FreightTemplate 对象") FreightTemplate freightTemplate) {
        try {
            freightTemplateService.add(freightTemplate);
            return HttpResult.ok("添加模板成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加模板失败!");
    }

    /**
     * @Description 批量删除运营模板
     * @param ids 模板id的数组
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody Long[] ids) {
        try {
            freightTemplateService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改运营模板
     * @param freightTemplate 修改的运营模板对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody(required = false) FreightTemplate freightTemplate) {
        try {
            freightTemplateService.update(freightTemplate);
            return HttpResult.ok("修改运营模板成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改运营模板失败!");
    }

    /**
     * @Description 根据ID查询运营模板
     * @param id 运营模板id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Long id) {
        return HttpResult.ok(freightTemplateService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param freightTemplate 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) FreightTemplate freightTemplate) {
        try {
            //分页查询
            PageResult result = freightTemplateService.findPage(pageNum, size, freightTemplate);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询运营模板异常");
        }
    }

    /**
     * @Description 分页查询
     * @param page 当前页码
     * @param rows 当前页条数
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageResult pageResult = freightTemplateService.findPage(page, rows);
        return pageResult;
    }

    /**
     * @Description 模糊查询运营模板
     * @param freightTemplate 运营模板对象
     * @return
     */
    @RequestMapping(value = "/findByWhere", method = RequestMethod.POST)
    public HttpResult findByWhere(@RequestBody(required = false) @ApiParam(value = "FreightTemplate 对象") FreightTemplate freightTemplate) {
        return HttpResult.ok(freightTemplateService.findByWhere(freightTemplate));
    }

}
